package com.zzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.common.exception.ServiceException;
import com.zzx.entity.TProductOrderType;
import com.zzx.mapper.TProductOrderTypeMapper;
import com.zzx.service.TProductOrderTypeService;
import com.zzx.utils.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zzx
 * @description 针对表【t_product_order_type】的数据库操作Service实现
 * @createDate 2024-11-30 17:14:43
 */
@Service
public class TProductOrderTypeServiceImpl extends ServiceImpl<TProductOrderTypeMapper, TProductOrderType>
        implements TProductOrderTypeService {
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(TProductOrderType param) {
        LambdaQueryWrapper<TProductOrderType> wrapper = new LambdaQueryWrapper<>(TProductOrderType.class);
        // 多租户拦截器会自动添加 company_id 条件
        wrapper.eq(TProductOrderType::getProductOrderTypeName, param.getProductOrderTypeName());
        if (ObjectUtils.isEmpty(param.getProductOrderTypeId())) {
            param.setProductOrderTypeId(IdUtil.getSnowflakeNextId());
        } else {
            wrapper.ne(TProductOrderType::getProductOrderTypeId, param.getProductOrderTypeId());
            EntityUtils.mergeNonNullProperties(this.getBaseMapper().selectById(param.getProductOrderTypeId()), param);
        }
        List<TProductOrderType> tProductOrderTypeList = this.getBaseMapper().selectList(wrapper);
        if (!tProductOrderTypeList.isEmpty()) {
            throw new ServiceException("订单类型名称已存在");
        }
        this.saveOrUpdate(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(TProductOrderType param) {
        this.removeById(param.getProductOrderTypeId());
    }

    @Override
    public List<TProductOrderType> selectBy(TProductOrderType param) {
        LambdaQueryWrapper<TProductOrderType> wrapper = new LambdaQueryWrapper<>(TProductOrderType.class);
        if (!ObjectUtils.isEmpty(param.getProductOrderTypeName())) {
            wrapper.like(TProductOrderType::getProductOrderTypeName, param.getProductOrderTypeName());
        }
        if (!ObjectUtils.isEmpty(param.getProductOrderTypeCode())) {
            wrapper.like(TProductOrderType::getProductOrderTypeCode, param.getProductOrderTypeCode());
        }
        if (!ObjectUtils.isEmpty(param.getProductOrderTypeId())) {
            wrapper.eq(TProductOrderType::getProductOrderTypeId, param.getProductOrderTypeId());
        }
        // 多租户拦截器会自加添加 company_id 条件
        List<TProductOrderType> tProductOrderTypes = this.getBaseMapper().selectList(wrapper);
        return tProductOrderTypes;
    }
}




